var str = "";

$(document).ready(function() {
	
	function gameOver(isGameOver) {
		if (isGameOver) {
			$("#game-guess-button").hide();
			$("#game-guess-input").hide();
			toast("You got it buddy !");
		} else {
			$("#game-guess-button").show();
			$("#game-guess-input").show();
		}
	}
	
	$("#home_newgame_button").click(function() {
		gameOver(false);
		if (window.gwjs) {
			try {
				var newGameJSON = window.gwjs.getNewGame();
				printGuesses(newGameJSON);	
			} catch (e) {
				toast(e.stack);
			}
		} else {
			toast("Android javascript interface has not been initialized.");
		}
	});
	
	$("#game-guess-button").click(function() {
		
		gameOver(false);
		if (window.gwjs) {
			try {
				var guess = $("#game-guess-input").val();
				var degree = 4;
				if (window.gwjs.isNCharactersLong(guess, degree)) {
					if (window.gwjs.isComprisedOnlyOfAlphabets(guess)) {
						if (window.gwjs.isMadeOfUniqueCharacters(guess)) {
							if (window.gwjs.isInTheEnglishDictionary(guess)) {
								if (window.gwjs.addGuess(guess)) {
									$("#game-guess-input").val("");
									var gameJSON = window.gwjs.getGame();
									printGuesses(gameJSON);
									if (window.gwjs.isWinner(guess)) {
										gameOver(true);
									}
								} else {
									toast("You've already tried " + guess);
								}
							} else {
								toast(guess + " is not in my dictionary. Can I see yours ?");
							}
						} else {
							if ("xxxx" === guess) {
								var gameJSON1 = window.gwjs.getGame();
								var game = $.parseJSON(gameJSON1);
								toast(game.keyword);										
							} else {
								toast("The guess has repeating alphabets.");	
							}
						}
					} else {
						toast("The guess should only contain alphabets");
					}
				} else {
					toast("The guess should be " + degree + " characters long");
				}
			} catch (e) {
				toast(e.stack);
			}
		} else {
			toast("Android javascript interface has not been initialized.");
		}
		$("#game-guess-input").focus();
	});
	
	/*
	$("#test").click(function() {
		try {
			var gameJSON = window.gwjs.getDummyGame();
			printGuesses(gameJSON);
		} catch (e) {
			toast(e.stack);
		}
	});
	*/
	
	function printGuesses(gameJSON) {
		clearGuesses();
		try {
			if (window.gwjs) {
				//toast("gameJSON:" + gameJSON);
				
				if (gameJSON) {
					var game = $.parseJSON(gameJSON);
					//toast("game.degree:" + game.degree);
					$.each( game.guesses,
							function(key, val) {
								printGuess(key, val);
							});
				} else {
					toast("game is null");
				}
			} else {
				toast("Android javascript interface has not been initialized.");
			}
		} catch(e) {
			toast("Encountered exception: " + e.stack);
		}
	}

	function printGuess(index, guess) {
		// $("#game-guesses").append(index + " : " + guess.word + " [X" + guess.clue.countX + ",0" + guess.clue.count0 + "]<br>");
		var w = guess.word;
		var wt = "";
		try {
			if (window.gwjs) {
				wt = window.gwjs.getWeight(w);
			} else {
				toast("Android javascript interface has not been initialized.");
			}
		} catch(e) {
			toast("Encountered exception: " + e.stack);
		}

		//$("#game-guesses").append(w + " " + wt + "<br>");
		
		$("#game-guesses").append('<div>');
		$("#game-guesses").append('<div class="game-guess-word-class">' + w + '</div>');
		$("#game-guesses").append('<div class="game-guess-clue-class">' + wt + '</div>');
		$("#game-guesses").append('</div>');
		/*
	    // clone div.template
	    var item = $("#game-guesses div.template").clone();
	    $(item).removeAttr("style").removeClass("template");
	         
	    // manipulate its contents
	    $(item).find("#game-guess-word").append(w);
	    $(item).find("#game-guess-clue").text(wt);
	    $(item).appendTo("#game-guesses");
	    $("#game-guesses").append($(item));
	    */
	}
	
	function clearGuesses() {
		$("#game-guesses").empty();
	}

	function recursiveFunction(key, val) {
        actualFunction(key, val);
        if (val instanceof Object) {
            $.each(val, function(k, v) {
                recursiveFunction(k, v);
            });
        }
    }

    function actualFunction(key, val) {
    	$("#game-guesses").append("[" + key + ":" + val + "]");
	}
	
	/*
	function appendGuess(guess) {
		$("#game-guesses").append("<b>");
		var guess = $("#game-guess-input").val();
		$("#game-guesses").append(guess);
		$("#game-guesses").append("</b><br/>");
	};
	*/
	
	function toast(text) {
		// alert("android javascript interface has not been initialized.");
		$("#game-hints").fadeIn(400, function() {
			$("#game-hints").html(text);
		}).delay(3000).fadeOut(400, function() {
			$("#game-hints").html("");
		}); // fade out after 3 seconds
		
	}
	
	function saveGame() {
		// TODO
		// call on back button press OR
		// HOME button press OR
		// Game over
	}
	
	$(".game-clue").mousein(function () {
		var factor = 1.2;
	    $(this).animate({
	        top: '-=' + $(this).height() / factor,
	        left: '-=' + $(this).width() / factor,
	        width: $(this).width() * factor
	    });
	});
	
	$(".game-clue").mouseout(function () {
		var factor = 1.2;
		$(this).animate({
	        top: '-=' + $(this).height() / factor,
	        left: '-=' + $(this).width() / factor,
	        width: $(this).width() * factor
	    });
	});
	
});
